package com.internetCafes.spms.common.AIRecognize;

import com.internetCafes.spms.common.utils.entity.Pic2WordItem;
import com.sms.common.util.http.HttpUtils;
import com.sms.common.util.http.ImageBase64;

import com.sms.common.util.hutool.json.JSONUtil;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author LinYe
 * @date 2020/10/14
 */
@Component
public class Pic2WordAIUtils {
    public static Pic2WordItem ScanPic(String filePath){
        String imgBase64 = ImageBase64.getImgStr(filePath);
        String host = "https://tysbgpu.market.alicloudapi.com";
        String path = "/api/predict/ocr_general";
        String method = "POST";
        String appcode = "1eaada1e91784c3b8a5f997d1fa79091";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        String bodys = "{\"image\":\""+ imgBase64 +"\",\"configure\":{\"min_size\":16,\"output_prob\":true,\"output_keypoints\":false,\"skip_detection\":false,\"without_predicting_direction\":false}}";

        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            //获取response的body
            String jsonStr = EntityUtils.toString(response.getEntity());
            Pic2WordItem entity = JSONUtil.toBean(jsonStr, Pic2WordItem.class);
            return entity;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
